package com.rao.crm.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.rao.crm.domain.Customer;
import com.rao.crm.domain.PageBean;
import com.rao.crm.service.CustomerService;
import com.rao.crm.utils.UploadUtils;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author Srao
 * @version 1.0
 * @className CustomerAction
 * @date 2019/9/19 15:16
 * @package com.rao.crm.web.action
 * @description
 */
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {

    private Customer customer = new Customer();
    private CustomerService customerService;
    /*分页的属性*/
    private Integer currPage = 1;
    private Integer pageSize = 3;
    /*保存图片的属性*/
    private String uploadFileName;//文件名称
    private String uploadContentType;//文件类型
    private File upload;//文件本身

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public void setCurrPage(Integer currPage) {
        if (null == currPage){
            currPage = 1;
        }
        this.currPage = currPage;
    }

    public void setPageSize(Integer pageSize) {
        if (null == pageSize){
            pageSize = 3;
        }
        this.pageSize = pageSize;
    }

    @Override
    public Customer getModel() {
        return customer;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    /*添加用户页面跳转*/
    public String saveUI(){
        return "saveUI";
    }

    public String save() throws IOException {
        /*保存文件到硬盘*/
        String file = UploadUtils.saveFile(uploadFileName, upload);
        /*把文件的路径存到数据库*/
        customer.setCust_image(file);

        /*保存客户*/
        customerService.save(customer);

        return "save";
    }

    public String findAll(){

        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
        /*添加查询条件*/
        if (null != customer.getCust_name()){//Restrictions 限制
            detachedCriteria.add(Restrictions.like("cust_name", "%"+customer.getCust_name()+"%"));
        }
        if (null != customer.getBaseDictSource()){
            if (customer.getBaseDictSource().getDict_id() != null && !"".equals(customer.getBaseDictSource().getDict_id())){
                detachedCriteria.add(Restrictions.eq("baseDictSource.dict_id", customer.getBaseDictSource().getDict_id()));
            }
        }
        if (null != customer.getBaseDictIndustry()){
            if (customer.getBaseDictIndustry().getDict_id() != null && !"".equals(customer.getBaseDictIndustry().getDict_id())){
                detachedCriteria.add(Restrictions.eq("baseDictIndustry.dict_id", customer.getBaseDictIndustry().getDict_id()));
            }
        }
        if (null != customer.getBaseDictLevel()){
            if (customer.getBaseDictLevel().getDict_id() != null && !"".equals(customer.getBaseDictLevel().getDict_id())){
                detachedCriteria.add(Restrictions.eq("baseDictLevel.dict_id", customer.getBaseDictLevel().getDict_id()));
            }
        }
        PageBean<Customer> pageBean = customerService.findAll(detachedCriteria, currPage, pageSize);
        ActionContext.getContext().getValueStack().push(pageBean);
        return "findAll";
    }

    public String delete(){
        //先查询，后删除
        customer = customerService.findById(customer.getCust_id());
        /*先删除图片*/
        UploadUtils.deleteFile(customer.getCust_image());
        /*再删除用户*/
        customerService.delete(customer);
        return "delete";
    }

    public String edit(){
        /*先查询再修改*/
        customer = customerService.findById(customer.getCust_id());
        /*取值的方式<s:property value="cust_name"  */
        ActionContext.getContext().getValueStack().push(customer);
        return "edit";
    }

    public String update() throws IOException {
        /*判断是否更新了图片*/
        if (null != upload){
            /*先删除图片*/
            UploadUtils.deleteFile(customer.getCust_image());
            /*更新图片*/
            String file = UploadUtils.saveFile(uploadFileName, upload);
            customer.setCust_image(file);
        }
        customerService.update(customer);
        return "update";
    }


    public String findAllByAjax() throws IOException {
        List<Customer> list = customerService.findAll();
        /*去掉多余的参数*/
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[] {"linkManSet", "baseDictSource", "baseDictIndustry", "baseDictLevel"});
        /*把数组转为JSON*/
        JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
        /*转到页面*/
        ServletActionContext.getResponse().setContentType("text/html; charset=utf-8");
        ServletActionContext.getResponse().getWriter().println(jsonArray.toString());

        return NONE;
    }
}
